<template>
  <div>
    <div class="flex justify-between items-center mt-2">
      <a-radio-group v-model:value="isDisplayed" button-style="solid">
        <a-radio-button :value="0">有效</a-radio-button>
        <a-radio-button :value="1">全部</a-radio-button>
      </a-radio-group>
      <div>
        <a-button type="primary" :disabled="store.radioKey" class="mr-2" @click="onSign()">签署</a-button>
        <a-button type="primary" :disabled="store.radioKey" class="mr-2" @click="onEdit()">新增申请单</a-button>
      </div>
    </div>
    <template v-if="applyList.length">
      <div style="height: calc(100vh - 260px)" class="mt-2 px-1.5 overflow-auto">
        <div v-for="(item, index) in applyList" :key="index" class="mt-5 mb-2 border border-solid rounded-sm">
          <div class="h-12 border-b border-solid flex items-center bg-thin">
            <div class="name truncate">
              <LabelValue class="ml-10" :width="230" tooltip :value="item?.categoryName" />
            </div>
            <div class="w-[160px]">
              <a-tag v-if="item.status === '未审核'" color="blue">未审核</a-tag>
              <a-tag v-if="item.status === '已审核'" color="green">已审核</a-tag>
              <a-tag v-if="item.status === '已打印'" color="green">已打印</a-tag>
              <a-tag v-if="item.status === '已作废'" color="red">已作废</a-tag>
              <a-tag v-if="item.status === '已签署'" color="red">已签署</a-tag>
            </div>
            <LabelValue class="ml-12" label="科室" tooltip :width="245" :value="item?.applyDeptName" />
            <LabelValue class="ml-1" label="医生" tooltip :width="150" :value="item?.applyDoctorName" />
            <LabelValue class="ml-12" label="申请时间" tooltip :width="227" :value="item?.applyTime" />
          </div>
          <div class="h-16 flex items-center justify-between">
            <div class="flex items-center">
              <LabelValue class="ml-12 mr-8" tooltip :width="360" :value="item.orderItemNames" />
              <LabelValue class="ml-12" label="审核人" tooltip :width="205" :value="item?.auditEmpName" />
              <LabelValue class="ml-12" label="审核时间" tooltip :width="250" :value="item?.auditTime" />
            </div>
            <div class="pr-4">
              <a-button type="link" size="small" :disabled="item.status !== '未审核' || item.status === '已签署'" @click="onEdit(item)"> 修改 </a-button>
              <BaseButton
                confirm-text="确认作废?"
                type="text"
                danger
                size="small"
                :disabled="item.status !== '未审核' || item.status === '已签署'"
                @click="onDelete(item)"
              >
                作废
              </BaseButton>
              <a-button type="link" size="small"> 查阅报告 </a-button>
              <a-button type="link" size="small"> 查阅影像 </a-button>
            </div>
          </div>
        </div>
      </div>
    </template>
    <template v-else>
      <a-empty />
    </template>
  </div>
  <Edit v-model:open="open" :row-data="currentRow" @success="queryList()" />
</template>
<script setup>
import { ref, watchEffect } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import LabelValue from '@/components/LabelValue'
import Edit from '@/views/out-doctor/workstation/TabTechnology/Edit.vue'

// import { getSystemConfig } from '@/utils/systemConfig'
import useStore from '../../store'

const store = useStore()
const currentRow = ref({})
const open = ref(false)
const loading = ref(false)
const applyList = ref([])
const tableListId = ref([])
const isDisplayed = ref(0)

const queryList = async () => {
  loading.value = true

  const params = {}
  if (isDisplayed.value) {
    params.brId = store.currentPatient.brId
  } else {
    params.key = store.currentPatient.inplsh
  }
  const { code, data, msg } = await post('/ris/apply/getApplyList', { ...params, applyPath: 1 })
  loading.value = false
  if (code !== 0) return message.error(msg || '请求失败')
  applyList.value = data
  tableListId.value = applyList.value.map((item) => item.orderItemIDs).flat()
}

const onEdit = async (row) => {
  currentRow.value = { ...row, ...store.currentPatient, applyPath: 1, key: store.currentPatient.inplsh, brId: store.currentPatient.brId }
  open.value = true
}

const onDelete = async (row) => {
  const { code, msg } = await post('/ris/apply/doChangeApplyStatus', { data: row.appSn })
  if (code !== 0) return message.error(msg)
  message.success('作废成功')
  queryList()
}
//签署
const onSign = async () => {
  if (!tableListId.value.length) {
    return message.error('不能为空')
  }
  const { code, msg } = await post('/inp/clinicalOrder/signedDoctor', { inpAdvIds: tableListId.value, signStatus: 1, inplsh: store.currentPatient.inplsh })
  if (code !== 0) return message.error(msg || '操作失败')
  message.success('签署成功')
  queryList()
}

// 打印单据
// const onPrint = async (item, dylx) => {
//   // /lis/lisPrint/printLisApply
//   const { code, msg, data } = await post('/lis/lisPrint/printLisRecord', { lsh: item.lsh, dylx })
//   if (code !== 0) return message.error(msg)
//   window.openExe?.rpt(data) //调用winform打印
// }

watchEffect(() => {
  if (store.currentPatient.inplsh) {
    queryList()
  }
})
</script>
<style lang="less" scoped>
.name {
  padding: 0 5px;
  font-weight: 700;
  font-size: 20px;
}
</style>
